<?php
namespace Home\Controller;
use Think\Controller;
class MouseController extends CommonController {
    public function index(){

    	$data = M('device a')
    		->where()
    		->field('a.name,b.status,a.code_num,a.cate_id')
    		->join("left join zm_device_cate b on a.cate_id=b.id")
    		->find();

    	$data['status'] = M('device_status')
    		->where('cate_id='.$data['cate_id'])
    		->field('status_name,id,is_next')
    		->select();

    	$map['pest'] = array('in',$data['pest']);

    	$data['pset_name'] = M('animal')
    		->where($map)
    		->field('name,id')
    		->select();

    	dump($data);exit();
    }

    public function statusNext(){
    	$cate_id = I('post.cate_id');
    }

    public function qrcodes($url="www.baidu.com",$level=3,$size=4)
  	{
       Vendor('phpqrcode.phpqrcode');
       $errorCorrectionLevel =intval($level) ;//容错级别 
       $matrixPointSize = intval($size);//生成图片大小 
       //生成二维码图片 
       // $object = new \QRcode();
       // \QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);

       // include 'phpqrcode.php';  
$value = 'http://www.cnblogs.com/txw1958/'; //二维码内容  
$errorCorrectionLevel = 'L';//容错级别  
$matrixPointSize = 6;//生成图片大小  
//生成二维码图片  
\QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = '__PUBLIC__/image/logo.png';//准备好的logo图片  
$QR = 'qrcode.png';//已经生成的原始二维码图

if($logo !== FALSE) {
  $QR = imagecreatefromstring(file_get_contents($QR));  
  $logo = imagecreatefromstring(file_get_contents($logo));  
  $QR_width = imagesx($QR);//二维码图片宽度  
  $QR_height = imagesy($QR);//二维码图片高度  
  $logo_width = imagesx($logo);//logo图片宽度  
  $logo_height = imagesy($logo);//logo图片高度  
  $logo_qr_width = $QR_width / 5;  
  $scale = $logo_width/$logo_qr_width;  
  $logo_qr_height = $logo_height/$scale;  
  $from_width = ($QR_width - $logo_qr_width) / 2;  
  //重新组合图片并调整大小  
  imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,  
  $logo_qr_height, $logo_width, $logo_height);  
}  
//输出图片  
imagepng($QR, 'helloweixin.png');  
echo '<img src="helloweixin.png">';

  	}

  	public function qrcode($data="www.baidu.com",$filename="niaho",$picPath=false,$logo="__PUBLIC__/image/logo.png",$size='4',$level='L',$padding=2,$saveandprint=false)

	{
     vendor("phpqrcode.phpqrcode");//引入工具包
     // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false

      $path = $picPath?$picPath:SITE_PATH."/Uploads/Picture/QRcode"; //图片输出路径
      //使用 “$path = $picPath?$picPath:SITE_PATH."\\Uploads\\Picture\\QRcode"; //图片输出路径” 的方式是有问题的。
      //如果路径不存在可以创建 mkdir($path);
     //在二维码上面添加LOGO

     if(empty($logo) || $logo=== false) { //不包含LOGO
         if ($filename==false) {
             \QRcode::png($data, false, $level, $size, $padding, $saveandprint); //直接输出到浏览器，不含LOGO
         }else{
             $filename=$path.'/'.$filename; //合成路径
             \QRcode::png($data, $filename, $level, $size, $padding, $saveandprint); //直接输出到浏览器，不含LOGO
         }
     }else { //包含LOGO
         if ($filename==false){
             //$filename=tempnam('','').'.png';//生成临时文件
            die('参数错误');
         }else {
             //生成二维码,保存到文件
             $filename = $path . '/' . $filename; //合成路径
         }
         \QRcode::png($data, $filename, $level, $size, $padding);
         $QR = imagecreatefromstring(file_get_contents($filename));
         $logo = imagecreatefromstring(file_get_contents($logo));
         $QR_width = imagesx($QR);
         $QR_height = imagesy($QR);
         $logo_width = imagesx($logo);
         $logo_height = imagesy($logo);
         $logo_qr_width = $QR_width / 5;
         $scale = $logo_width / $logo_qr_width;
         $logo_qr_height = $logo_height / $scale;
         $from_width = ($QR_width - $logo_qr_width) / 2;
         imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
         if ($filename === false) {
             Header("Content-type: image/png");
             imagepng($QR);
         } else {
             if ($saveandprint === true) {
                 imagepng($QR, $filename);
                 header("Content-type: image/png");//输出到浏览器
                 imagepng($QR);
             } else {
                 imagepng($QR, $filename);
             }
         }
     }
     return '/'.$filename; //输出时加上前面的/
  }
}